package com.start.ad.admin;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.ad.entity.StartAdContentEntity;
import com.start.ad.service.StartAdContentService;
import com.start.common.api.Result;
import com.start.common.dataImport.DataImportEntity;
import com.start.common.dataImport.ImportExportUtils;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import com.start.security.annotations.DataPermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/start/ad/content")
@RegisterMenu(value = "广告内容管理", parentId = "", systemCode = "start:ad:content", component = "views/start/ad/content/index", path = "/start/ad/content")
public class StartAdContentController {
    private static final String LOG_TITLE = "广告内容管理";

    @Autowired
    private StartAdContentService service;

    /**
     * 列表
     */
    @GetMapping("/page")
    @DataPermission("views/start/ad/content/index")
    @PreAuthorize("@pas.hasPermission('start:ad:content:info')")
    public Result<?> list(Page<StartAdContentEntity> page, StartAdContentEntity entity) {
        QueryWrapper<StartAdContentEntity> queryWrapper = DataQueryConstructor.createEntity(StartAdContentEntity.class);
        return Result.ok(service.page(page, queryWrapper));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @StartLog(value = LOG_TITLE, action = "查询")
    @PreAuthorize("@pas.hasPermission('start:ad:content:info')")
    @RegisterPermission(value = "start:ad:content:info", name = "查看")
    public Result<?> info(@PathVariable("id") String id) {
        return Result.ok(service.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @StartLog(value = LOG_TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('start:ad:content:add')")
    @RegisterPermission(value = "start:ad:content:add", name = "添加")
    public Result<?> save(@RequestBody StartAdContentEntity entity) {
        service.save(entity);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/updateById")
    @StartLog(value = LOG_TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('start:ad:content:edit')")
    @RegisterPermission(value = "start:ad:content:edit", name = "修改")
    public Result<?> update(@RequestBody StartAdContentEntity entity) {
        service.updateById(entity);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteById/{id}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @PreAuthorize("@pas.hasPermission('start:ad:content:delete')")
    @RegisterPermission(value = "start:ad:content:delete", name = "删除")
    public Result<?> delete(@PathVariable("id") String id) {
        service.removeById(id);
        return Result.ok();
    }

    @GetMapping("/downloadTemplate")
    @DataPermission("physical/medicalCore/list")
    @PreAuthorize("@pas.hasPermission('start:ad:content:downloadTemplate')")
    @RegisterPermission(value = "start:ad:content:downloadTemplate", name = "下载导入模板")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "标签导入模板.xls";
        try {
            ImportExportUtils.createImportTemplate(StartAdContentEntity.class, response, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/exportExcel")
    @PreAuthorize("@pas.hasPermission('start:ad:content:exportExcel')")
    @RegisterPermission(value = "start:ad:content:exportExcel", name = "导出excel")
    @DataPermission("views/zm/common/tag/index")
    public void exportExcel(HttpServletResponse response) {
        String fileName = LOG_TITLE + "导出.xls";
        QueryWrapper<StartAdContentEntity> queryWrapper = DataQueryConstructor.createEntity(StartAdContentEntity.class);
        List<StartAdContentEntity> list = service.list(queryWrapper);
        ImportExportUtils.exportExcel(StartAdContentEntity.class, list, response, fileName);
    }


    @PostMapping("/importExcel")
    @PreAuthorize("@pas.hasPermission('start:ad:content:importExcel')")
    @RegisterPermission(value = "start:ad:content:importExcel", name = "导入excel")
    public Result<?> importExcel(@RequestBody DataImportEntity entity) {
        List<String> filePaths = entity.getFilePath();
        if (filePaths == null || filePaths.size() < 1) return Result.ok();
        List<StartAdContentEntity> dateList = ImportExportUtils.importExcelFile(StartAdContentEntity.class, filePaths.get(0));
        if (dateList.size() < 1) {
            return Result.ok();
        }
        service.saveBatch(dateList, 30);
        log.info("文件导入:" + JSON.toJSONString(dateList));
        return Result.ok();
    }


}
